//---------------------------------------------------------------------------
#ifndef mqLib
#define mqLib 0.1
//---------------------------------------------------------------------------

#include <map>
#include <set>
#include <list>
#include <stack>
#include <deque>
#include <vector>
#include <memory>
#include <algorithm>

//---------------------------------------------------------------------------

#define mqUpdate(Type, Source, Expression) \
  { \
    typedef Type::value_type T; \
    Type::iterator Iter; \
    for (Iter = Source.begin(); Iter != Source.end(); ++Iter) \
    { \
      T &i = *Iter; \
      Expression \
    } \
  }

#define mqFrom(Type, Source) \
  { \
    typedef Type::value_type T;   \
    typedef class \
    { \
      public: \
        static T Create(const T &i) \
        { \
          return i; \
        } \
    } mqCast; \
    typedef Type::value_type NewT; \
    typedef Type::iterator TIter;  \
    std::vector<T> mqDataSource; \
    for (TIter iter = Source.begin(); iter != Source.end(); ++iter) \
      mqDataSource.push_back(*iter); \

#define mqEnd }

//---------------------------------------------------------------------------

#define mqWhere(Condition) \
    { \
      std::vector<T> mqTempData; \
      for (size_t j = 0, cnt = mqDataSource.size(); j < cnt; ++j) \
      { \
        const T &i = mqDataSource[j]; \
        if (Condition) \
          mqTempData.push_back(i); \
      } \
      mqDataSource = mqTempData; \
    }

#define mqRange(iFrom, iTo) \
    { \
      std::vector<T> mqTempData; \
      for (size_t I = 0, cnt = mqDataSource.size(); I < cnt; ++I) \
      { \
        if (I >= (iFrom) && I <= (iTo)) \
          mqTempData.push_back(i); \
      } \
      mqDataSource = mqTempData; \
    }

#define mqType(Type) \
    { \
      std::vector<T> mqTempData; \
      for (size_t I = 0, cnt = mqDataSource.size(); I < cnt; ++I) \
        if (dynamic_cast<Type>(mqDataSource[i])) \
          mqTempData.push_back(i); \
      mqDataSource = mqTempData; \
    }

#define mqUnion(Type, Source) \
    for (Type::iterator Iter = Source.begin(); Iter != Source.end(); ++Iter) \
      mqDataSource.push_back(*Iter);

#define mqDistinct \
    for (int I = mqDataSource.size() - 1; I > 0; --I) \
      for (int J = I - 1; J >= 0; --J) \
        if (mqDataSource[I] == mqDataSource[J]) \
        { \
          mqDataSource.erase(mqDataSource.begin() + I); \
          break; \
        }

//---------------------------------------------------------------------------

#define mqEach(Expression) \
    for (size_t I = 0, cnt = mqDataSource.size(); I < cnt; ++I) \
    { \
      T &i = mqDataSource[I]; \
      Expression \
    }

//---------------------------------------------------------------------------

#define mqReverse \
    std::reverse(mqDataSource.begin(), mqDataSource.end());

#define mqSort(Comparator) \
    { \
      class mqSortClass \
      { \
        public: \
          __inline bool Compare(const T &i, const T &j) const { Comparator } \
          void Sort(std::vector<T> &Data, T elem) const \
          { \
            const size_t DataSize = Data.size(); \
            for (size_t i = 0, cnt = DataSize - 1; i < cnt; ++i) \
            { \
              size_t MinIdx = i; \
              for (size_t j = i + 1; j < DataSize; ++j) \
                if (Compare(Data[j], Data[MinIdx])) \
                  MinIdx = j; \
              elem = Data[i]; \
              Data[i] = Data[MinIdx]; \
              Data[MinIdx] = elem; \
            } \
          } \
      }; \
      mqSortClass mqSortObject; \
      T TempBuffer; \
      mqSortObject.Sort(mqDataSource, TempBuffer); \
    }

//---------------------------------------------------------------------------

#define mqCast(NewType, InitCode) \
    typedef NewType NewT; \
    typedef class \
    { \
      public: \
        static NewType Create(T i) \
        { \
          NewType j; \
          InitCode \
          return j;   \
        } \
    } mqCast;

#define mqNew(NewType) \
    typedef NewType NewT; \
    typedef calss \
    { \
      public: \
        static NewType Create(T i) \
        { \
          return NewType(i); \
        } \
    } mqCast;

//---------------------------------------------------------------------------

#define mqToVector(Destination) \
    { \
      Destination.clear(); \
      Destination.reserve(mqDataSource.size()); \
      for (size_t I = 0; I < mqDataSource.size(); ++I) \
        Destination.push_back(mqCast::Create(mqDataSource[I])); \
    } \
  }

#define mqToArray(Destination, Size) \
    { \
      Size = mqDataSource.size(); \
      Destination = (NewT *)malloc(sizeof(NewT) * mqDataSource.size()); \
      for (size_t I = 0; I < mqDataSource.size(); ++I) \
        Destination[I] = mqCast::Create(mqDataSource[I]); \
    } \
  }

//---------------------------------------------------------------------------

#define mqMax(Comparator, Result) \
    if (mqDataSource.size()) \
    { \
      size_t MaxIdx = 0; \
      T &i = mqDataSource[MaxIdx]; \
      for (size_t I = 1; I < mqDataSource.size(); ++I) \ \
      { \
        const T &j = mqDataSource[I]; \
        if (!(Comparator)) \
        { \
          MaxIdx = I; \
          &i = mqDataSource[MaxIdx]; \
        } \
      } \
      Result = mqDataSource[MaxIdx]; \
    } \
  }

#define mqMin(Comparator, Result) \
    if (mqDataSource.size()) \
    { \
      size_t MinIdx = 0; \
      T &i = mqDataSource[MinIdx]; \
      for (size_t I = 1; I < mqDataSource.size(); ++I) \ \
      { \
        const T &j = mqDataSource[I]; \
        if (Comparator) \
        { \
          MinIdx = I; \
          &i = mqDataSource[MinIdx]; \
        } \
      } \
      Result = mqDataSource[MinIdx]; \
    }

#define mqFirst(Result) \
    if (mqDataSource.size()) \
      Result = mqDataSource.front(); \

#define mqLast(Result) \
    if (mqDataSource.size()) \
      Result = mqDataSource.back(); \

#define mqSum(Expression, Result) \
    mqEach(Result += Expression) \

#define mqAvg(Expression, Result) \
    mqSum(Expression, Result) \
    if (mqDataSource.size()) \
      Result /= mqDataSource.size()

#define mqCount(iResult) \
    iResult = mqDataSource.size();

#define mqAny(Expression, bResult) \
    bResult = false; \
    for (size_t I = 0, cnt = mqDataSource.size(); I < cnt; ++I) \
    { \
      const T &i = mqDataSource[I]; \
      if (Expression) \
      { \
        bResult = true; \
        break; \
      } \
    }

#define mqAll(Expression, bResult) \
    bResult = true; \
    for (size_t I = 0, cnt = mqDataSource.size(); I < cnt; ++I) \
    { \
      const T &i = mqDataSource[I]; \
      if (!(Expression)) \
      { \
        bResult = false; \
        break; \
      } \
    }

#define mqHas(Elem, bResult) \
    mqAny(i == Elem, bResult)

//---------------------------------------------------------------------------
#endif
